package com.hustzp.com.xichuangzhu.me;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.utils.DialogFactory;

/**
 * Created by Administrator on 2016/6/5.
 */
public class EditUserInfoActivity extends MyBaseActivity {

    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_user_info);

        initToolbar();
        initViews();
    }

    private void initToolbar() {
        TextView backTxt = (TextView) findViewById(R.id.tv_nav_txt);
        backTxt.setText(getString(R.string.pernalinfomation));

        TextView appTitle = (TextView) findViewById(R.id.tv_title_txt);
        appTitle.setText(getIntent().getStringExtra("appTitle"));


        TextView actionText = (TextView) findViewById(R.id.tv_action_txt);
        actionText.setVisibility(View.VISIBLE);
        actionText.setText("保存");
        actionText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 编辑用户名，不可为空
                int editFlag = getIntent().getIntExtra("editFlag", 0);
                if (editFlag == 1) {
                    if (TextUtils.isEmpty(editText.getText().toString())) {
                        DialogFactory.hintDialog(EditUserInfoActivity.this, getString(R.string.pernalinfomation_name_is_not_empity));
                        return;
                    }

                    if (editText.getText().toString().length() > 30) {
                        DialogFactory.hintDialog(EditUserInfoActivity.this, getString(R.string.pernalinfomation_name_limit));
                        return;
                    }
                } else {
                    if (TextUtils.isEmpty(editText.getText().toString())) {
                        DialogFactory.hintDialog(EditUserInfoActivity.this, getString(R.string.pernalinfomation_sigined_is_not_empity));
                        return;
                    }

                    if (editText.getText().toString().length() > 20) {
                        DialogFactory.hintDialog(EditUserInfoActivity.this, getString(R.string.pernalinfomation_sigined_limit));
                        return;
                    }
                }

                if (editFlag == 1) {
                    AVUser.getCurrentUser().setUsername(editText.getText().toString());
                } else if (editFlag == -1) {
                    AVUser.getCurrentUser().put("desc", editText.getText().toString());
                }

                AVUser.getCurrentUser().saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e == null) {
                            finish();
                        } else {
                            Toast.makeText(EditUserInfoActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }

    private void initViews() {
        editText = (EditText) findViewById(R.id.et_user_info);
        // 设置用户信息
        editText.setText(getIntent().getStringExtra("userInfo"));
    }
}
